package com.hhyx.execl.controller;

import com.hhyx.execl.model.entity.Before;
import com.hhyx.execl.model.entity.SystemMessage;
import com.hhyx.execl.model.params.BeforeParams;
import com.hhyx.execl.model.params.SystemSearchParam;
import com.hhyx.execl.model.result.R;
import com.hhyx.execl.model.result.ReturnPage;
import com.hhyx.execl.service.BeforeFileService;
import com.hhyx.execl.service.FileImportService;
import com.hhyx.execl.service.SystemMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;

/**
 * @author MarkerCCC
 * @date 2019/6/14 - 15:05
 * @email markerccc@163.com
 */
@Slf4j
@RestController
@RequestMapping("")
public class FileController {

    private List<SystemMessage> list;

    @Autowired
    private SystemMessageService systemMessageService;
    @Autowired
    private BeforeFileService beforeFileService;
    @Autowired
    private FileImportService fileImportService;

    @GetMapping("get")
    public R<Boolean> get(HttpServletResponse response) {
        systemMessageService.readFolder(response);
        log.error("------>执行成功");
        return new R<>(true);
    }

    @GetMapping("getBeforePage")
    public R<ReturnPage<Before>> beforePage(BeforeParams param) {
        return new R<>(beforeFileService.pageList(param));
    }




    @GetMapping("pageList")
    public R<ReturnPage<SystemMessage>> pageList(SystemSearchParam param) {
        return new R(systemMessageService.pageList(param));
    }


    @GetMapping("getResult")
    public R<Integer> getResult(HttpServletResponse response) {
        return new R(systemMessageService.getResult(response));
    }

    @GetMapping("getResult1")
    public ServletOutputStream getResult1(HttpServletResponse response) {
        return systemMessageService.getResult1(response);
    }

    @GetMapping("/again")
    public R<Boolean> again(HttpServletResponse response) {
        String path = "D:\\MarkWork\\again";
        fileImportService.readFolder(path, LocalDate.now().toString(), response);
        log.error("------>执行成功");
        return new R<>(true);
    }

    @GetMapping("fileGetResult")
    public R<Integer> fileGetResult(HttpServletResponse response) {
        return new R(fileImportService.getResult(response));
    }

    @GetMapping("filegetResult1")
    public ServletOutputStream fileGetResult1(HttpServletResponse response) {
        return fileImportService.getResult1(response);
    }

}
